package com.lc.zpyh.ui.fragment;

import android.content.res.ColorStateList;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;

import com.google.android.material.tabs.TabLayout;
import com.lc.zpyh.R;
import com.lc.zpyh.app.AppFragment;
import com.lc.zpyh.ui.activity.MainActivity;
import com.lc.zpyh.ui.fragment.order.OrderListFragment;
import com.lc.zpyh.util.constant.EvenConstant;
import com.lc.zpyh.util.eventbus.Event;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;
import butterknife.BindView;

public class OrderFoodFragment extends AppFragment<MainActivity>{
    @BindView(R.id.slidingTabLayout)
    TabLayout slidingTabLayout;
    @BindView(R.id.viewpager)
    ViewPager viewPager;
    private List<Fragment> tabFragments = new ArrayList<>();//碎片集合
    private List<String> tabIndicators = new ArrayList<>();//title集合
    private ContentPagerAdapter mAdapter;
    private List<String> ids = new ArrayList<>();

    @Override
    public void onViewCreated(@NonNull @NotNull View view, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        //订单状态(0未支付,1支付成功,2配餐中,4配送中,5已存柜,6已接收,7已失效,8已删除(用户不可见))9 无柜存餐待确认 10 已取消 11 失效已取出12支付失败13订单无效
        tabIndicators.add("全部");
        tabIndicators.add("待付款");
        tabIndicators.add("配餐中");
        tabIndicators.add("配送中");
        tabIndicators.add("待取餐");
        tabIndicators.add("待评价");
        tabIndicators.add("已完成");
        tabIndicators.add("售后订单");
        tabFragments.add(OrderListFragment.newInstance(""));
        tabFragments.add(OrderListFragment.newInstance("0"));
        tabFragments.add(OrderListFragment.newInstance("2"));
        tabFragments.add(OrderListFragment.newInstance("4"));
        tabFragments.add(OrderListFragment.newInstance("5"));
        tabFragments.add(OrderListFragment.newInstance("10000"));
        tabFragments.add(OrderListFragment.newInstance("6"));
        tabFragments.add(OrderListFragment.newInstance("10"));
        ids.add("");ids.add("0");ids.add("2");ids.add("4");ids.add("5");ids.add("10000");ids.add("6");ids.add("10");

        mAdapter = new ContentPagerAdapter(getChildFragmentManager());
        viewPager.setAdapter(mAdapter);

        slidingTabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
        slidingTabLayout.setTabRippleColor(ColorStateList.valueOf(Color.parseColor("#00000000")));
        slidingTabLayout.setupWithViewPager(viewPager);

        //底部导航栏切换监听
        slidingTabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
//                cur_poi = tab.getPosition();
                OrderListFragment fragment= (OrderListFragment)mAdapter.getItem(tab.getPosition());
                fragment.showMessageFromActivity(ids.get(tab.getPosition()));
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_orderfood;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {

    }
    class ContentPagerAdapter extends FragmentPagerAdapter {

        public ContentPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return tabFragments.get(position);
        }

        @Override
        public int getCount() {
            return tabIndicators.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return tabIndicators.get(position);
        }
    }


    @Override
    protected boolean isRegisterEventBus() {
        return true;
    }

    @Override
    protected void receiveEvent(Event event) {
        super.receiveEvent(event);
        if (event.getCode() == EvenConstant.TABONE) {
            slidingTabLayout.getTabAt(1).select();
            viewPager.setCurrentItem(1);
        }else if (event.getCode() == EvenConstant.TABALL){
            slidingTabLayout.getTabAt(0).select();
            viewPager.setCurrentItem(0);
        }else if (event.getCode() == EvenConstant.TABTWO){
            slidingTabLayout.getTabAt(2).select();
            viewPager.setCurrentItem(2);
        }
        else if (event.getCode() == EvenConstant.TABTHREE){
            slidingTabLayout.getTabAt(3).select();
            viewPager.setCurrentItem(3);
        }
        else if (event.getCode() == EvenConstant.TABFOUR){
            slidingTabLayout.getTabAt(5).select();
            viewPager.setCurrentItem(5);
        }
        else if (event.getCode() == EvenConstant.TABFIVE){
            slidingTabLayout.getTabAt(7).select();
            viewPager.setCurrentItem(7);
        }else if (event.getCode() == EvenConstant.PAYSTAUTS) {
            slidingTabLayout.getTabAt(0).select();
            viewPager.setCurrentItem(0);
        }
    }
}
